
#include <QCoreApplication>
#include <QFile>

#include "configfile.h"

ConfigFile::ConfigFile(QString path, QObject *parent) : QObject(parent)
{
    this->status = ConfigFile::NO_ERROR;
    this->settingsFilePath = path;
    if(QFile(path).exists()) {
        this->settings = new QSettings(path, QSettings::IniFormat);
        this->settings->setIniCodec("UTF-8");
    } else {
        this->autoSetDefaultConfig();
    }
}

ConfigFile::~ConfigFile()
{
    delete this->settings;
}

void ConfigFile::setValue(const QString &key, const QVariant &value)
{
    this->settings->setValue(key, value);
}

QVariant ConfigFile::value(const QString &key, const QVariant &defaultValue) const
{
    return this->settings->value(key, defaultValue);
}

ConfigFile::Status ConfigFile::autoSetDefaultConfig()
{
    this->settings = new QSettings(this->settingsFilePath, QSettings::IniFormat);
    this->settings->setIniCodec("UTF-8");

    /* set default value */
    this->settings->setValue(QString(USER_WEB_URL),         "http://filegeli.timechicken.cc");
    this->settings->setValue(QString(USER_NAME),            "");
    this->settings->setValue(QString(USER_PASSWORD),        "");
    this->settings->setValue(QString(USER_AUTO_LOGIN),      "false");
    this->settings->setValue(QString(USER_SAVE_PASSWORD),   "false");
    this->settings->setValue(QString(USER_PHPSESSID),       "");
    /* set default value done */
    this->settings->sync();
    return ConfigFile::NO_ERROR;
}


